<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>Title</title>
<!--    <link rel="stylesheet" href="css/reset.css">-->
    <link rel="stylesheet" href="layui/css/layui.css">
    <link rel="stylesheet" href="css/index.css">
</head>
<body>


<div class="layui-container container">
  <div class="layui-card">
    <div class="layui-card-header"><h1>todo list</h1></div>
    <div class="layui-card-body cont">
      <!--
      <div class="item">
        <input type="checkbox" class="laytable-cell-checkbox">
        <a href="javascript:void (0)">hahahahah</a>
      </div>
      -->
    </div>
  </div>

  <form class="layui-form" action="">
    <div class="layui-form-item">
      <label class="layui-form-label">输入框</label>
      <div class="layui-input-block">
        <input type="text" name="content" required  lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-input-block">
        <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
      </div>
    </div>
  </form>
</div>


<script src="js/jquery.js"></script>
<script src="layui/layui.js"></script>
<script>

  layui.use('form', function(){
    let form = layui.form;
    form.on('submit(formDemo)', function(data){
      $.ajax({
        method: 'POST',
        url: `https://api.yizhanketang.cn/api/v1/todos`,
        data: data.field,
        success(res) {
          location.reload()
        },
        error(e) {
          layer.msg('缺少content');
        }
      })
      return false;
    });
  });

  $(function () {
    $.ajax({
      method:'GET',
      url:`https://api.yizhanketang.cn/api/v1/todos`,
      success(res){
        const {data} = res
        data.forEach((item)=>{
          const {completed,content,objectId } = item
          $('.cont').append(`
            <div class="item">
            <input type="checkbox" ${completed ? 'checked' : ''} class="laytable-cell-checkbox">
            <a href="about.html?id=${objectId}">${content}</a>
            </div>`)
        })
      },
      error(e){
        console.log(e)
      }
    })
  })

</script>
</body>
</html>